home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-08-10 | 6.4 KB | 234 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CTCPResponder.cp ©1995-1998 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- // Responders represent the endpoints that handle the actual connection to and
- // interaction with remote clients.
-
- #include "CTCPResponder.h"
- #include "CTCPResponderThread.h"
-
- #include <UNetworkFactory.h>
-
- #include <LWindow.h>
- #include <LApplication.h>
-
- #include <Sound.h>
-
-
- // ===========================================================================
- // • Resource IDs
- // ===========================================================================
-
- const PP_PowerPlant::ResIDT PPob_TCPTerminalWindow = 129;
- const PP_PowerPlant::PaneIDT pTerminal = 'TERM';
-
-
- // ===========================================================================
-
- #pragma mark ••• CTCPResponder •••
-
-
- // ---------------------------------------------------------------------------
- // • CTCPResponder
- // ---------------------------------------------------------------------------
-
- CTCPResponder::CTCPResponder(
- LCommander* inSuper)
- : LSingleDoc(inSuper)
- {
- mTerminalPane = nil;
- mEndpoint = nil;
- mLineBufferIndex = 0;
- mLineMode = true;
- mEchoCharacters = true;
- mResponderThread = nil;
- mQuitWhenDone = false;
- mSaveOption = 0;
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CTCPResponder
- // ---------------------------------------------------------------------------
-
- CTCPResponder::~CTCPResponder()
- {
- if (mEndpoint) {
- delete mEndpoint;
- mEndpoint = nil;
- }
- }
-
- // ---------------------------------------------------------------------------
- // • Accept
- // ---------------------------------------------------------------------------
- // Accept a connection from a remote machine on this endpoint. This endpoint
- // will do all the dirty work.
- // The display window is also opened at this time.
- //
- // This routine creates an endpoint, but does NOT bind it!
- // The endpoint is automagically bound to the port on which incomming connection
- // was received via the 'AcceptIncoming' routine inside the thread.
-
- void
- CTCPResponder::Accept(CSimpleTCPServer * inServer)
- {
- BuildServerWindow();
-
- mEndpoint = PP_PowerPlant::UNetworkFactory::CreateTCPEndpoint();
-
- mResponderThread = new CTCPResponderThread(mEndpoint, mTerminalPane, this, inServer);
- mResponderThread->Resume();
- }
-
- // ---------------------------------------------------------------------------
- // • IsIdle
- // ---------------------------------------------------------------------------
-
- Boolean
- CTCPResponder::IsIdle()
- {
- if (mEndpoint == nil)
- return true;
- PP_PowerPlant::EEndpointState netState = mEndpoint->GetState();
- return ((netState == PP_PowerPlant::endpoint_Idle) || (netState == PP_PowerPlant::endpoint_Unbound));
- }
-
-
- // ---------------------------------------------------------------------------
- // • ServerThreadDied
- // ---------------------------------------------------------------------------
-
- void
- CTCPResponder::ServerThreadDied()
- {
- mResponderThread = nil;
-
- //Re-issue the quit command when we are ready.
- if (mQuitWhenDone) {
- PP_PowerPlant::LApplication* theApp = dynamic_cast<PP_PowerPlant::LApplication*>(GetSuperCommander());
- if (theApp) {
- theApp->DoQuit(mSaveOption);
- }
- }
-
- delete this;
- }
-
- // ---------------------------------------------------------------------------
- // • Disconnect
- // ---------------------------------------------------------------------------
-
- void
- CTCPResponder::Disconnect()
- {
- if (mResponderThread) {
- mResponderThread->StartDisconnect();
- }
- }
-
- // ---------------------------------------------------------------------------
- // • BindCompleted
- // ---------------------------------------------------------------------------
-
- void
- CTCPResponder::BindCompleted()
- {
- PP_PowerPlant::LInternetAddress* theAddress = mEndpoint->GetRemoteHostAddress();
- SetWindowTitle(*theAddress);
- delete theAddress;
- }
-
- // ---------------------------------------------------------------------------
- // • BindFailed
- // ---------------------------------------------------------------------------
-
- void
- CTCPResponder::BindFailed()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • HandleKeyPress
- // ---------------------------------------------------------------------------
- // Server side does not allow user interaction with the server window...
- // We simply beep and swallow the event.
-
- Boolean
- CTCPResponder::HandleKeyPress(
- const EventRecord& /* inKeyEvent */)
- {
- ::SysBeep(30);
- return true;
- }
-
- // ---------------------------------------------------------------------------
- // • BuildSessionWindow
- // ---------------------------------------------------------------------------
- // Build the window for this connection session.
-
- void
- CTCPResponder::BuildServerWindow()
- {
- mWindow = PP_PowerPlant::LWindow::CreateWindow(PPob_TCPTerminalWindow, this);
- mTerminalPane = dynamic_cast <CTerminalPane*> (mWindow->FindPaneByID(pTerminal));
- ThrowIfNil_ (mTerminalPane);
- }
-
- // ---------------------------------------------------------------------------
- // • SetWindowTitle
- // ---------------------------------------------------------------------------
-
- void
- CTCPResponder::SetWindowTitle(
- PP_PowerPlant::LInternetAddress & inAddress)
- {
- if (mWindow) {
- Str255 titleStr;
- inAddress.GetIPDescriptor(titleStr);
- mWindow->SetDescriptor(titleStr);
- }
- }
-
- // ---------------------------------------------------------------------------
- // • AllowSubRemoval
- // ---------------------------------------------------------------------------
- // If user tries to close the connection window, interpret this as a request
- // to close the connection. The window is not immediately closed; rather
- // it is closed when the connection actually goes away.
-
- Boolean
- CTCPResponder::AllowSubRemoval(
- LCommander *inSub)
- {
- if ((inSub == mWindow) && !IsIdle()) {
- Disconnect();
- return false;
- }
- else
- return LSingleDoc::AllowSubRemoval(inSub);
- }
-
- // ---------------------------------------------------------------------------
- // • AttemptQuitSelf
- // ---------------------------------------------------------------------------
- // Close our connection before quitting. We re-issue the quit command via
- // the ServerThreadDied method once we are done.
-
- Boolean
- CTCPResponder::AttemptQuitSelf(
- SInt32 inSaveOption)
- {
- if (mQuitWhenDone) {
- return LCommander::AttemptQuitSelf(inSaveOption);
- } else {
- mQuitWhenDone = true;
- mSaveOption = inSaveOption;
-
- Disconnect();
- return false;
- }
- }
-
-
-